package app.fugema.shop.main.order.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import app.fugema.shop.main.goods.model.SkuPriceResult;
import app.fugema.shop.main.home.model.GoodsDetailBean;
import app.fugema.shop.main.order.model.ShopCartProduct;
import app.fugema.shop.main.order.model.ShopCartResult;

import com.base.common.model.http.jackSon.JacksonUtils;
import com.base.common.netBeanPackage.BaseApiResult;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.UIUtils;
import com.base.common.viewmodel.BaseViewModel;
import com.base.common.viewmodel.NetWorkFunction;
import app.fugema.shop.http.HttpService;
import app.fugema.shop.main.order.model.ShopModifyCountResult;
import app.fugema.shop.main.order.model.ShoppingCountBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShoppingCartVM extends BaseViewModel {


    public ShoppingCartVM(@NonNull Application application)
    {
        super(application);
    }


    /**
     *
     * @param skusCount
     * @param userKey
     * @return
     */
    public MutableLiveData<LiveDataWrapper<ShopModifyCountResult>> updateCartChange(ShoppingCountBean skusCount, String userKey)
    {

       String jsonStr = JacksonUtils.transBean2Json(skusCount);
       return send(HttpService.getHttpServer().updateCart(jsonStr,userKey));
    }


    /**
     * 添加商品到购物车
     * @param skuId
     * @param count
     * @param userKey
     * @return
     */
    public MutableLiveData<LiveDataWrapper<BaseApiResult>> addGoodsToCart(String skuId,int count,String userKey)
    {
        return send(HttpService.getHttpServer().addGoodsToCart(skuId,count,userKey));
    }

    public MutableLiveData<LiveDataWrapper<GoodsDetailBean>> getSkuList(ShopCartProduct bean) {

        GoodsDetailBean.DataBean dataBean = new GoodsDetailBean.DataBean();
        /*
        dataBean.setShopId(bean.getShopId());
        dataBean.setShopName(bean.getShopName());
        dataBean.setProdId(bean.getProdId());
        dataBean.setProdName(bean.getProdName());
        dataBean.setPrice(bean.getPrice());
        dataBean.setOriPrice(bean.getOriPrice());
        dataBean.setPic(bean.getPic());
        */
        /*
        return send(HttpService.getHttpServer().getSkuList(10).map(new NetWorkFunction<BaseListDataWrapper<GoodsDetailBean.DataBean.SkuListBean>, GoodsDetailBean>() {
            @Override
            public GoodsDetailBean onSuccess(BaseListDataWrapper<GoodsDetailBean.DataBean.SkuListBean> skuListBean) {
                GoodsDetailBean goodsDetailBean = new GoodsDetailBean();
                setBeanCode(goodsDetailBean);
                goodsDetailBean.setData(dataBean);
                dataBean.setSkuList(skuListBean.getData());
                for (GoodsDetailBean.DataBean.SkuListBean listBean : dataBean.getSkuList())
                {
                    //bean.getSkuId() == listBean.getSkuId()
                    if ( true )
                    {
                        listBean.setSelected(true);
                        break;
                    }
                }
                return goodsDetailBean;
            }
            @Override
            public GoodsDetailBean onFailure(BaseListDataWrapper<GoodsDetailBean.DataBean.SkuListBean> skuListBean) {
                return setBeanCode(new GoodsDetailBean());
            }
        }));
        */
        return null;
    }

    /**
     *  查询 用户购物车信息
     * @param userKey
     * @return
     */
    public MutableLiveData<LiveDataWrapper<List<ShopCartProduct>>> queryCartProduct(String userKey)
    {
        int includeSB = 0;
        //);
        return send(HttpService.getHttpServer().getCartProduct(includeSB,userKey)
                                               .map(new NetWorkFunction<ShopCartResult, List<ShopCartProduct>>()
                                               {
                                            @Override
                                            public List<ShopCartProduct> onSuccess(ShopCartResult shopCartResult)
                                            {
                                                List<ShopCartProduct> productList = new ArrayList<>();
                                                if (UIUtils.isNotEmpty(shopCartResult.getShop()))
                                                {
                                                    for (List<ShopCartProduct> shopCartProducts : shopCartResult.getShop())
                                                    {
                                                        for (ShopCartProduct tempGoods : shopCartProducts)
                                                        {
                                                            productList.add( tempGoods );
                                                        }
                                                    }
                                                }
                                                return productList;
                                            }
                                            @Override
                                            public boolean isEmptyData(ShopCartResult data)
                                            {
                                                return UIUtils.isEmpty(data.getShop());
                                            }
                                            @Override
                                            public List<ShopCartProduct> onFailure(ShopCartResult shopCartResult)
                                            {
                                                return new ArrayList<ShopCartProduct>();
                                            }
                                        }));
    }

    /**
     * 删除 单个商品
     * @param skuId
     * @param userKey
     * @return
     */
    public MutableLiveData<LiveDataWrapper<BaseApiResult>> deleteCartProduct(String skuId , String userKey)
    {
        List<String> list = new ArrayList<>();
        list.add(skuId);
        return deleteCartBySkuIds(list , userKey);
    }

    /**
     * 删除 购物车 商品
     * @param skuIdList
     * @param userKey
     * @return
     */
    public MutableLiveData<LiveDataWrapper<BaseApiResult>> deleteCartBySkuIds(List<String> skuIdList ,String userKey)
    {
        StringBuffer sb = new StringBuffer();
        for (String skuId : skuIdList)
        {
             if(UIUtils.isNotEmpty( sb.toString() ))
             {
                 sb.append(",");
             }
             sb.append(String.valueOf( skuId ));
        }
        return send(HttpService.getHttpServer().deleteCartBySkuId(sb.toString(),userKey));
    }

    public MutableLiveData<LiveDataWrapper<SkuPriceResult>> getSkuInfo(int productId, String userKey)
    {
        Map<String,Object> params = new HashMap<>();
        if( productId != 0 )
        {
            params.put("productId", productId);
        }
        if(UIUtils.isNotEmpty( userKey ))
        {
            params.put("userkey", userKey);
        }
        return send(HttpService.getHttpServer().getSkuInfo(params));
    }

}
